<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="问卷标题(问卷名称,比如大学生睡眠情况调查问卷)" prop="questionnaireName">
      <el-input v-model="dataForm.questionnaireName" placeholder="问卷标题(问卷名称,比如大学生睡眠情况调查问卷)"></el-input>
    </el-form-item>
    <el-form-item label="问卷创建时间" prop="creationDate">
      <el-input v-model="dataForm.creationDate" placeholder="问卷创建时间"></el-input>
    </el-form-item>
    <el-form-item label="问卷的创建者" prop="createdBy">
      <el-input v-model="dataForm.createdBy" placeholder="问卷的创建者"></el-input>
    </el-form-item>
    <el-form-item label="问卷状态(1表示问卷已发送，2表示删除问卷，3表示问卷已开启，4表示终止问卷，5表示问卷处于关闭状态)" prop="status">
      <el-input v-model="dataForm.status" placeholder="问卷状态(1表示问卷已发送，2表示删除问卷，3表示问卷已开启，4表示终止问卷，5表示问卷处于关闭状态)"></el-input>
    </el-form-item>
    <el-form-item label="可以选择问卷风格(1代表一种风格，2代表一种风格等)" prop="style">
      <el-input v-model="dataForm.style" placeholder="可以选择问卷风格(1代表一种风格，2代表一种风格等)"></el-input>
    </el-form-item>
    <el-form-item label="问卷的题目(比如1.吃什么？2.写什么?)" prop="problemList">
      <el-input v-model="dataForm.problemList" placeholder="问卷的题目(比如1.吃什么？2.写什么?)"></el-input>
    </el-form-item>
    <el-form-item label="限制的问卷答卷次数" prop="limitNumber">
      <el-input v-model="dataForm.limitNumber" placeholder="限制的问卷答卷次数"></el-input>
    </el-form-item>
    <el-form-item label="限定的问卷答题时间" prop="limitTime">
      <el-input v-model="dataForm.limitTime" placeholder="限定的问卷答题时间"></el-input>
    </el-form-item>
    <el-form-item label="问卷发布时间(问卷发送)" prop="releaseTime">
      <el-input v-model="dataForm.releaseTime" placeholder="问卷发布时间(问卷发送)"></el-input>
    </el-form-item>
    <el-form-item label="最新修改日期" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="最新修改日期"></el-input>
    </el-form-item>
    <el-form-item label="问卷结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="问卷结束时间"></el-input>
    </el-form-item>
    <el-form-item label="问卷开启时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="问卷开启时间"></el-input>
    </el-form-item>
    <el-form-item label="0代表未删除，1代表已删除（伪删除）" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="0代表未删除，1代表已删除（伪删除）"></el-input>
    </el-form-item>
    <el-form-item label="0代表不是优质答卷，1代表是优质答卷(优质答卷判断位)" prop="isExcellent">
      <el-input v-model="dataForm.isExcellent" placeholder="0代表不是优质答卷，1代表是优质答卷(优质答卷判断位)"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          questionnaireName: '',
          creationDate: '',
          createdBy: '',
          status: '',
          style: '',
          problemList: '',
          limitNumber: '',
          limitTime: '',
          releaseTime: '',
          updateTime: '',
          endTime: '',
          startTime: '',
          isDeleted: '',
          isExcellent: ''
        },
        dataRule: {
          questionnaireName: [
            { required: true, message: '问卷标题(问卷名称,比如大学生睡眠情况调查问卷)不能为空', trigger: 'blur' }
          ],
          creationDate: [
            { required: true, message: '问卷创建时间不能为空', trigger: 'blur' }
          ],
          createdBy: [
            { required: true, message: '问卷的创建者不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '问卷状态(1表示问卷已发送，2表示删除问卷，3表示问卷已开启，4表示终止问卷，5表示问卷处于关闭状态)不能为空', trigger: 'blur' }
          ],
          style: [
            { required: true, message: '可以选择问卷风格(1代表一种风格，2代表一种风格等)不能为空', trigger: 'blur' }
          ],
          problemList: [
            { required: true, message: '问卷的题目(比如1.吃什么？2.写什么?)不能为空', trigger: 'blur' }
          ],
          limitNumber: [
            { required: true, message: '限制的问卷答卷次数不能为空', trigger: 'blur' }
          ],
          limitTime: [
            { required: true, message: '限定的问卷答题时间不能为空', trigger: 'blur' }
          ],
          releaseTime: [
            { required: true, message: '问卷发布时间(问卷发送)不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '最新修改日期不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '问卷结束时间不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '问卷开启时间不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '0代表未删除，1代表已删除（伪删除）不能为空', trigger: 'blur' }
          ],
          isExcellent: [
            { required: true, message: '0代表不是优质答卷，1代表是优质答卷(优质答卷判断位)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/questionnaire/questionnaire/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.questionnaireName = data.questionnaire.questionnaireName
                this.dataForm.creationDate = data.questionnaire.creationDate
                this.dataForm.createdBy = data.questionnaire.createdBy
                this.dataForm.status = data.questionnaire.status
                this.dataForm.style = data.questionnaire.style
                this.dataForm.problemList = data.questionnaire.problemList
                this.dataForm.limitNumber = data.questionnaire.limitNumber
                this.dataForm.limitTime = data.questionnaire.limitTime
                this.dataForm.releaseTime = data.questionnaire.releaseTime
                this.dataForm.updateTime = data.questionnaire.updateTime
                this.dataForm.endTime = data.questionnaire.endTime
                this.dataForm.startTime = data.questionnaire.startTime
                this.dataForm.isDeleted = data.questionnaire.isDeleted
                this.dataForm.isExcellent = data.questionnaire.isExcellent
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/questionnaire/questionnaire/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'questionnaireName': this.dataForm.questionnaireName,
                'creationDate': this.dataForm.creationDate,
                'createdBy': this.dataForm.createdBy,
                'status': this.dataForm.status,
                'style': this.dataForm.style,
                'problemList': this.dataForm.problemList,
                'limitNumber': this.dataForm.limitNumber,
                'limitTime': this.dataForm.limitTime,
                'releaseTime': this.dataForm.releaseTime,
                'updateTime': this.dataForm.updateTime,
                'endTime': this.dataForm.endTime,
                'startTime': this.dataForm.startTime,
                'isDeleted': this.dataForm.isDeleted,
                'isExcellent': this.dataForm.isExcellent
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
